/*
NoBuffer Notepad Copyright (C) 2015  Mahmoud Jaoune

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FILETHREAD_H
#define FILETHREAD_H

#include <QThread>
#include <QWidget>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QFile>
#include <QSettings>
#include "blockdb.h"
#include "textedit.h"

#define BLOCKSIZE 200000

class NBN_FileThread : public QThread
{
    Q_OBJECT

public:
    NBN_FileThread(QWidget *parent, QString fileName, QString currentTab, NBN_TextEdit *textEdit);
    QFile *currentFile;
    unsigned long long blockSize;
    int blockIndex;
    int nBlocks;
    QString loadedTab;
    NBN_TextEdit *currentTextEdit;

    QChar NBN_GetCharAt(int index, QString buffer);
    QString NBN_GetBlockFromFile(unsigned long long offset, size_t blockSize, size_t allocSize, QFile *fileIO);
    int NBN_GetCurrentLineNumber(int reachedSize, QFile *fileIO);


private:
    void run();

public slots:
    void NBN_LoadBlock(unsigned int index);
    void NBN_CloseThread(QString currentTab);
    void NBN_SaveFile(QString fileName, BlockDB database);
    void NBN_SaveAsFile(QString fileName, QString newFileName, BlockDB database);

    void on_UndoAction(QString fileName);
    void on_RedoAction(QString fileName);
    void on_CutAction(QString fileName);
    void on_CopyAction(QString fileName);
    void on_PasteAction(QString fileName);

signals:
    void NBN_BufferReady(QString buffer);
    void NBN_FileLoadProcess(unsigned int bytesLoaded, unsigned int totalBytes);
    void NBN_FileLoadFinished();
    void NBN_NumberOfBlocks(int nBlocks, QString currentTab);
    void NBN_SendClearText();
};

#endif // FILETHREAD_H
